package com.orange.common.base.httpConfig;

import com.orange.common.constant.TraceConstant;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;
import org.slf4j.MDC;

import java.io.IOException;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2023/6/27 16:47
 */
public class HttpClientTraceIdInterceptor implements HttpRequestInterceptor {
    @Override
    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
        String traceId = MDC.get(TraceConstant.MDC_TRACE);
        //当前线程调用中有traceId，则将该traceId进行透传
        if (traceId != null) {
            //添加请求体
            httpRequest.addHeader(TraceConstant.MDC_TRACE, traceId);
        }
    }
}